package com.kalix.ynpetroleum.networkDevice.biz;

import com.google.gson.JsonObject;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ynpetroleum.networkDevice.api.biz.INetworkDeviceBeanService;
import com.kalix.ynpetroleum.networkDevice.api.dao.INetworkDeviceBeanDao;
import com.kalix.ynpetroleum.networkDevice.api.model.PostRequestUtil;
import com.kalix.ynpetroleum.networkDevice.entities.NetworkDeviceBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *@ Author  ZhaoHang
 * @Date  2019/8/28
 **/
public class NetworkDeviceBeanServiceImpl extends GenericBizServiceImpl<INetworkDeviceBeanDao, NetworkDeviceBean> implements INetworkDeviceBeanService {
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ynpetroleum.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ynpetroleum.portconf");
    @Override
    public String getValue() {
        String ifOctetsBody = "{" +
                "\"metricses\": [{" +
                "\"metricsDefPath\": \"MetricsDefinition.mo=\\\"Line\\\",name=\\\"ifOctets\\\"\"," +
                "\"arguments\": \"\"," +
                "\"sortable\": true" +
                "}]," +
                "\"filters\": [{" +
                "\"grades\": [1, 2, 3]," +
                "\"pdmEngine\": \"defaultEngine\"," +
                "\"moType\": \"Line\"," +
                "\"moFilter\": \"1=1\"," +
                "\"permissionMoType\": \"Line\"," +
                "\"permissionMoFilter\": \"1=1\"" +
                "}]" +
                "}";
        List<List<String>> ifOctetsList = getAll(ifOctetsBody);
        String ifPercentBody = "{" +
                "\"metricses\": [{" +
                "\"metricsDefPath\": \"MetricsDefinition.mo=\\\"Line\\\",name=\\\"ifOctets\\\"\"," +
                "\"arguments\": \"\"," +
                "\"sortable\": true" +
                "}]," +
                "\"filters\": [{" +
                "\"grades\": [1, 2, 3]," +
                "\"pdmEngine\": \"defaultEngine\"," +
                "\"moType\": \"Line\"," +
                "\"moFilter\": \"1=1\"," +
                "\"permissionMoType\": \"Line\"," +
                "\"permissionMoFilter\": \"1=1\"" +
                "}]" +
                "}";
        List<List<String>> ifPercentList = getAll(ifPercentBody);
        String all = "";
        return null;
    }



    public List<List<String>> getAll(String body){
        String getAll = PostRequestUtil.HttpPostWithJson("http://"+wsIp+":"+wsPort+"/v2/metrics/topN?top=5&role=admin",body);
        JSONObject jsonObjectAll  = new JSONObject(getAll);
        JSONArray bodyJsonArray = jsonObjectAll.getJSONArray("body");
        List<String> name = new ArrayList<>();
        List<String> ip = new ArrayList<>();
        List<String> value = new ArrayList<>();
        List<List<String>> list = new ArrayList<>();
        for(int i= 0;i<bodyJsonArray.length();i++){
            JSONObject jsonObject  = new JSONObject(bodyJsonArray.get(i).toString());
            JSONObject moJsonObject  = new JSONObject(jsonObject.get("mo").toString());
            name.add(moJsonObject.get("name").toString());
            ip.add(moJsonObject.get("ip").toString());
            JSONArray listJsonArray = new JSONArray(jsonObject.get("topNMetricsList").toString());
            JSONObject getJsonObject  = new JSONObject( listJsonArray.get(0).toString());
            value.add(getJsonObject.get("value").toString());
        }
        list.add(name);
        list.add(ip);
        list.add(value);
        return list;
    }
}
